﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class ChucVuBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();

        public List<CHUCVU> LayTatCa()
        {
            return DB.CHUCVUs.ToList();
        }

        public void ThemCV(CHUCVU chucvu)
        {
            DB.CHUCVUs.InsertOnSubmit(chucvu);
            DB.SubmitChanges();
        }

        public string KiemTraTonTai(int maCV, string tenCV)
        {
            if (DB.CHUCVUs.Any(a => a.MaCV == maCV))
                return "Mã Chức Vụ";
            if (DB.CHUCVUs.Any(b => b.TenCV == tenCV))
                return "Tên Chức Vụ";
            else
                return null;
        }

        public bool KiemTraTenCV(int macv, string tencv)
        {
            if (DB.CHUCVUs.Any(a => a.MaCV == macv && a.TenCV != tencv))
                return true;
            else
                return false;
        }

        public CHUCVU LayCV(int macv)
        {
            CHUCVU cv = new CHUCVU();
            cv = DB.CHUCVUs.Single(a => a.MaCV == macv);
            return cv;
        }

        public void XoaCV(int macv)
        {
            CHUCVU cv = LayCV(macv);
            DB.CHUCVUs.DeleteOnSubmit(cv);
            DB.SubmitChanges();
        }

        public void SuaCV(CHUCVU cvn)
        {
            CHUCVU cvo = DB.CHUCVUs.Single(a => a.MaCV == cvn.MaCV);
            cvo.TenCV = cvn.TenCV;
            cvo.MoTa = cvn.MoTa;
            DB.SubmitChanges();
        }

        public List<string> LayCVTuNV(int manv)
        {

            List<string> cv = (from a in DB.CT_CHUCVUs
                               where a.MaNV == manv
                               select a.CHUCVU.TenCV).ToList();
            return cv;
        }
    }
}
